package com.hjw.weixun.condition;


import java.util.ArrayList;
import java.util.List;


/**
 * 封装查询条件的类
 *  例：QueryCondition cond = new QueryCondition();
 *      cond.putCondition("name","jeason");		
 *      cond.putCondition("code",Expression.OP_LIKE,"aaa"); 
 *      cond.addOrder("order1");
 *      cond.addOrder("order2","desc"); 
 * 
 * 
 */
public class QueryCondition {
	/**
	 * 查询表达式的集合
	 */
	private List<Expression> conditions = new ArrayList<Expression>();
	
	/**
	 * 排序字段的集合
	 */
	private List<Order> orders =  new ArrayList<Order>();
	
	/**
	 * 记录开始的位置
	 */
	private int start = 0;
	
	/**
	 * 记录结束的位置
	 */
	private int limit = -1;

	/**
	 * 添加一条查询表达式
	 * @param name
	 * @param value
	 * @return
	 */
	public QueryCondition putCondition(String name,Object value){
		
		return putCondition(name,"=",value);
	}
	
	/**
	 * 添加一条查询表达式
	 * @param name
	 * @param op
	 * @param value
	 * @return
	 */
	public QueryCondition putCondition(String name,String op,Object value){
		Expression exp = new Expression(name,op,value);
		
		return putCondition(exp);
	}
	
    /**
     * 添加一条查询表达式
     * @param exp
     * @return
     */
	public QueryCondition putCondition(Expression exp){
		this.conditions.add(exp);
		return this;
	}
	
	/**
	 * 清除所有的查询表达式
	 * @return
	 */
	public QueryCondition clearConditions(){
		this.conditions.clear();
		return this;
	}
	
	/**
	 * 添加一个排序字段
	 * @param name
	 * @return
	 */
	public QueryCondition addOrders(String name){
		this.orders.add(new Order(name));
		return this;
	}
	
	/**
	 * 添加一个排序字段
	 * @param name
	 * @param order
	 * @return
	 */
	public QueryCondition addOrders(String name,String order){
		this.orders.add(new Order(name,order));
		return this;
	}
	
	/**
	 * 清除所有的排序条件
	 * @return
	 */
	public QueryCondition clearOrders(){
		this.orders.clear();
		return this;
	}
	
	
	/**
	 * 下面是getters 和 setters  
	 */
	public List<Expression> getConditions() {
		return conditions;
	}

	public void setConditions(List<Expression> conditions) {
		this.conditions = conditions;
	}

	public List<Order> getOrders() {
		return orders;
	}

	public void setOrders(List<Order> orders) {
		this.orders = orders;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}
	
}
